﻿package plugin.emailui.contorl
{
    import com.core.*;
    import com.greensock.*;
    import com.sh.game.consts.*;
    import com.sh.game.net.*;
    import com.sh.game.util.*;
    import consts.*;
    import flash.events.*;
    import flash.geom.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.emailui.*;
    import plugin.taskdialog.consts.*;
    import pluginfw.nio.*;
    import uiresource.*;
    import view.*;
    import view.mornExtends.*;
    import vo.*;

    public class EmailUIctrl extends EmailUI
    {
        private var mUIBase:Email;
        private var _isshow:Boolean = true;
        private var eid:int;
        private var state:int;
        private var _cfgitem:Object;
        private var _nouse:Boolean = false;
        private var _itemArr:Array;
        private var cfgitem:Object;
        private var _isFirst:Boolean = true;
        private var _mPage:int = 1;
        private var _tPage:int = 1;
        private var _emailObj:Object = null;
        private var _emailList:Array;
        private var timeline:TweenMax;

        public function EmailUIctrl(param1:Email)
        {
            this._itemArr = [];
            this.cfgitem = ConfigDictionary.data.item;
            this._emailList = [];
            this.mUIBase = param1;
            return;
        }// end function

        override protected function initializeView() : void
        {
            super.initializeView();
            var _loc_1:* = Control.create(EmailControl, this);
            ModelProxy.addObserver("email", _loc_1);
            btnClose.clickHandler = new Handler(this.close);
            ModelProxy.change("data", "emailIsInit", true);
            PluginManager.instance.sendToServer("email", CMDEmail.GetEmail, null);
            this.clear();
            return;
        }// end function

        private function initEvent() : void
        {
            shanchu_btn.clickHandler = new Handler(this.deleteAll);
            quanqu_btn.clickHandler = new Handler(this.getAll);
            btndelete.clickHandler = new Handler(this.deleteOne);
            btnSure.clickHandler = new Handler(this.getEmail);
            xiangqian_btn.clickHandler = new Handler(this.leftPage);
            xianghou_btn.clickHandler = new Handler(this.rigthPage);
            txtDesc.mouseChildren = true;
            txtDesc.mouseEnabled = true;
            txtDesc.addEventListener(TextEvent.LINK, this.linkHandler);
            return;
        }// end function

        private function linkHandler(event:TextEvent) : void
        {
            var _loc_2:Array = null;
            var _loc_3:String = null;
            var _loc_4:String = null;
            var _loc_5:Array = null;
            var _loc_6:int = 0;
            var _loc_7:Object = null;
            _loc_2 = event.text.split("&");
            switch(int(_loc_2[0]))
            {
                case 1:
                {
                    _loc_3 = _loc_2[1];
                    if (_loc_3 == "shenzhuangHCPL")
                    {
                        _loc_7 = ConfigDictionary.data.sysnpc[87];
                        PluginManager.instance.sendToServer("taskdialog", CMDTaskdialog.Fly, {x:_loc_7.x, y:_loc_7.y, tostageid:_loc_7.stageid});
                        ModelProxy.change("data", "taskfindpath", _loc_7.npcid);
                    }
                    PluginManager.instance.changePlugin(_loc_3, new Point(100, 100), 1, null);
                    break;
                }
                case 2:
                {
                    _loc_4 = _loc_2[1];
                    _loc_5 = _loc_4.split("#");
                    ModelProxy.change("zone", "autoMove", {x:_loc_5[0], y:_loc_5[1], tostageid:_loc_5[2], onhorse:true});
                    break;
                }
                case 3:
                {
                    _loc_6 = _loc_2[1];
                    Server.instance.send(114, 7, _loc_6, 0, ConnectType.SYSTEM);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function getEmail() : void
        {
            var _loc_1:Point = null;
            var _loc_2:Output = null;
            if (GuideView.getInstance().checkIsDo(GuideType.GUIDE_EMAIL1))
            {
                GuideView.getInstance().doGuide(GuideType.GUIDE_EMAIL1);
            }
            else if (GuideView.getInstance().checkIsDo(GuideType.GUIDE_EMAIL2))
            {
                GuideView.getInstance().doGuide(GuideType.GUIDE_EMAIL2);
            }
            if (this.eid > 0)
            {
                _loc_1 = items.localToGlobal(new Point(0, 0));
                ModelProxy.change("data", "itempoint", _loc_1);
                _loc_2 = new Output();
                _loc_2.writeShort(1);
                _loc_2.writeVarInt(this.eid, false);
                PluginManager.instance.sendToBufferServer("email", CMDEmail.SubmitEmail, _loc_2);
            }
            return;
        }// end function

        private function deleteOne() : void
        {
            var _loc_1:Output = null;
            if (this.eid > 0)
            {
                if (this.state < 2)
                {
                    ModelProxy.change("movetips", "error", "有未读邮件或邮件未提取");
                    return;
                }
                _loc_1 = new Output();
                _loc_1.writeShort(1);
                _loc_1.writeVarInt(this.eid, false);
                PluginManager.instance.sendToBufferServer("email", CMDEmail.Delete, _loc_1);
            }
            return;
        }// end function

        private function deleteAll() : void
        {
            var _loc_3:int = 0;
            var _loc_1:* = new Output();
            var _loc_2:int = 6;
            if (elist.length < 6)
            {
                _loc_2 = elist.length;
            }
            _loc_1.writeShort(_loc_2);
            while (_loc_3 < _loc_2)
            {
                
                _loc_1.writeVarInt(elist.array[_loc_3].emailId, false);
                if (elist.array[_loc_3].state < 2)
                {
                    ModelProxy.change("movetips", "error", "有未读邮件或邮件未提取");
                    return;
                }
                _loc_3++;
            }
            PluginManager.instance.sendToBufferServer("email", CMDEmail.Delete, _loc_1);
            return;
        }// end function

        private function getAll() : void
        {
            var _loc_3:int = 0;
            var _loc_1:* = new Output();
            var _loc_2:int = 6;
            if (elist.length < 6)
            {
                _loc_2 = elist.length;
            }
            _loc_1.writeShort(_loc_2);
            while (_loc_3 < _loc_2)
            {
                
                _loc_1.writeVarInt(elist.array[_loc_3].emailId, false);
                _loc_3++;
            }
            PluginManager.instance.sendToBufferServer("email", CMDEmail.SubmitEmail, _loc_1);
            return;
        }// end function

        private function close() : void
        {
            ModelProxy.change("plugin", "close", "email");
            return;
        }// end function

        public function openRefresh() : void
        {
            this.getYe(this._mPage);
            return;
        }// end function

        public function initShow(param1:Array) : void
        {
            param1.sortOn("state", "time");
            var _loc_2:int = 0;
            while (_loc_2 < param1.length)
            {
                
                this._emailList.push(param1[_loc_2]);
                _loc_2++;
            }
            elist.array = this._emailList;
            elist.renderHandler = new Handler(this.emailRender);
            items.array = [];
            items.renderHandler = new Handler(this.rmRender);
            this.initEvent();
            this.getYe();
            return;
        }// end function

        private function leftPage() : void
        {
            if (this._mPage == 1)
            {
                return;
            }
            var _loc_1:String = this;
            var _loc_2:* = this._mPage - 1;
            _loc_1._mPage = _loc_2;
            this.getYe(this._mPage);
            return;
        }// end function

        private function rigthPage() : void
        {
            if (this._mPage == this._tPage)
            {
                return;
            }
            var _loc_1:String = this;
            var _loc_2:* = this._mPage + 1;
            _loc_1._mPage = _loc_2;
            this.getYe(this._mPage);
            return;
        }// end function

        private function showPage() : void
        {
            var _loc_1:Array = [];
            var _loc_2:* = (this._mPage - 1) * 6;
            while (_loc_2 < (this._mPage - 1) * 6 + 6)
            {
                
                if (this._emailList[_loc_2])
                {
                    _loc_1.push(this._emailList[_loc_2]);
                }
                _loc_2++;
            }
            elist.array = _loc_1;
            return;
        }// end function

        private function getYe(param1:int = 1) : void
        {
            this._tPage = Math.ceil(this._emailList.length / 6);
            if (this._tPage == 0)
            {
                this._tPage = 1;
            }
            if (param1 >= this._tPage)
            {
                this._mPage = this._tPage;
            }
            else
            {
                this._mPage = param1;
            }
            yeshu_name.text = this._mPage + "/" + this._tPage;
            this.showPage();
            return;
        }// end function

        private function showEmail(param1:Object) : void
        {
            var _loc_2:Clip = null;
            var _loc_3:Component = null;
            var _loc_11:Image = null;
            var _loc_12:Array = null;
            var _loc_13:String = null;
            var _loc_14:Array = null;
            var _loc_15:Object = null;
            var _loc_16:Output = null;
            if (param1 == null)
            {
                return;
            }
            money1.text = "0";
            money2.text = "0";
            money3.text = "0";
            money4.text = "0";
            this._emailObj = param1;
            this.eid = param1.emailId;
            this.state = param1.state;
            for each (_loc_3 in this._itemArr)
            {
                
                _loc_2 = _loc_3.getChildByName("guang") as Clip;
                _loc_2.visible = false;
            }
            if (this._itemArr[param1.index])
            {
                _loc_2 = this._itemArr[param1.index].getChildByName("guang") as Clip;
                _loc_11 = this._itemArr[param1.index].getChildByName("read") as Image;
                _loc_11.visible = true;
                _loc_2.visible = true;
            }
            labName.text = param1.from;
            labTitle.text = param1.title;
            var _loc_4:* = new Date(param1.time * 1000);
            var _loc_5:String = "00";
            var _loc_6:String = "00";
            var _loc_7:String = "00";
            _loc_5 = _loc_4.getHours() + "";
            _loc_6 = _loc_4.getMinutes() + "";
            _loc_7 = _loc_4.getSeconds() + "";
            if (_loc_4.getHours() < 10)
            {
                _loc_5 = "0" + _loc_4.getHours();
            }
            if (_loc_4.getMinutes() < 10)
            {
                _loc_6 = "0" + _loc_4.getMinutes();
            }
            if (_loc_4.getSeconds() < 10)
            {
                _loc_7 = "0" + _loc_4.getSeconds();
            }
            time.text = TimeDataFormatUtil.getTimeStr1(param1.time) + "    " + _loc_5 + ":" + _loc_6 + ":" + _loc_7;
            txtDesc.text = "    " + param1.desc;
            var _loc_8:* = param1.items;
            var _loc_9:Array = [];
            var _loc_10:Boolean = false;
            if (_loc_8 && _loc_8 != "" && _loc_8 != "null")
            {
                _loc_12 = _loc_8.split(";");
                for each (_loc_13 in _loc_12)
                {
                    
                    _loc_14 = _loc_13.split("#");
                    _loc_15 = new Object();
                    _loc_15.itemid = _loc_14[0];
                    _loc_15.count = _loc_14[1];
                    if (_loc_14[0] == 1)
                    {
                        money1.text = _loc_14[1];
                        _loc_10 = true;
                        continue;
                    }
                    if (_loc_14[0] == 2)
                    {
                        money4.text = _loc_14[1];
                        _loc_10 = true;
                        continue;
                    }
                    if (_loc_14[0] == 3)
                    {
                        money2.text = _loc_14[1];
                        _loc_10 = true;
                        continue;
                    }
                    if (_loc_14[0] == 6)
                    {
                        money3.text = _loc_14[1];
                        _loc_10 = true;
                        continue;
                    }
                    if (this.cfgitem[_loc_15.itemid])
                    {
                        _loc_15.icon = this.cfgitem[_loc_15.itemid].icon;
                    }
                    _loc_9.push(_loc_15);
                    _loc_10 = true;
                }
            }
            if (_loc_10 == true && param1.state < 2)
            {
                btnSure.visible = true;
                this.play();
            }
            else
            {
                btnSure.visible = false;
                this.stop();
            }
            if (_loc_10)
            {
                box_items.visible = true;
                box_items2.visible = true;
            }
            else
            {
                box_items.visible = false;
                box_items2.visible = false;
            }
            if (param1.state != 2)
            {
                items.array = _loc_9;
            }
            else
            {
                items.array = [];
            }
            items.renderHandler = new Handler(this.rmRender);
            if (param1.state == 0)
            {
                _loc_16 = new Output();
                _loc_16.writeVarInt(param1.emailId, false);
                PluginManager.instance.sendToBufferServer("email", CMDEmail.Look, _loc_16);
            }
            return;
        }// end function

        private function clear() : void
        {
            items.array = [];
            this.eid = 0;
            this.state = 0;
            labName.text = "";
            labTitle.text = "";
            time.text = "";
            txtDesc.text = "";
            yeshu_name.text = "1/1";
            money1.text = "0";
            money2.text = "0";
            money3.text = "0";
            money4.text = "0";
            return;
        }// end function

        private function rmRender(param1:Component, param2:int) : void
        {
            var _loc_3:Object = null;
            var _loc_4:Object = null;
            if (param2 < items.length)
            {
                _loc_3 = items.array[param2];
                _loc_4 = null;
                if (this.cfgitem[_loc_3.itemid])
                {
                    _loc_4 = ItemObject.create(this.cfgitem[_loc_3.itemid]);
                    _loc_4.count = _loc_3.count;
                }
                (param1 as FlashItem).data = _loc_4;
            }
            return;
        }// end function

        private function choseEmail(event:MouseEvent) : void
        {
            event.stopPropagation();
            this.showEmail(event.currentTarget.dataSource);
            return;
        }// end function

        private function delEmail(event:MouseEvent) : void
        {
            event.stopPropagation();
            if (event.currentTarget.parent.dataSource.state < 2)
            {
                ModelProxy.change("movetips", "error", "有未读邮件或邮件未提取");
                return;
            }
            var _loc_2:* = new Output();
            _loc_2.writeShort(1);
            _loc_2.writeVarInt(event.currentTarget.parent.dataSource.emailId, false);
            PluginManager.instance.sendToBufferServer("email", CMDEmail.Delete, _loc_2);
            return;
        }// end function

        private function emailRender(param1:Component, param2:int) : void
        {
            var _loc_3:Object = null;
            var _loc_4:Label = null;
            var _loc_5:Label = null;
            var _loc_6:Label = null;
            var _loc_7:Image = null;
            var _loc_8:Button = null;
            var _loc_9:Clip = null;
            var _loc_10:Image = null;
            var _loc_11:Image = null;
            if (param2 < elist.length)
            {
                _loc_3 = elist.array[param2];
                _loc_4 = param1.getChildByName("fname") as Label;
                _loc_5 = param1.getChildByName("name") as Label;
                _loc_6 = param1.getChildByName("desc") as Label;
                _loc_7 = param1.getChildByName("img") as Image;
                _loc_8 = param1.getChildByName("close") as Button;
                _loc_9 = param1.getChildByName("guang") as Clip;
                _loc_10 = param1.getChildByName("read") as Image;
                _loc_11 = param1.getChildByName("bz") as Image;
                if (_loc_3.state == 0)
                {
                    _loc_10.visible = false;
                }
                else
                {
                    _loc_10.visible = true;
                }
                if (_loc_3.state < 2 && _loc_3.items != "" || _loc_3.state < 2 && _loc_3.items != null)
                {
                    _loc_11.visible = true;
                }
                else
                {
                    _loc_11.visible = false;
                }
                _loc_3.index = param2;
                _loc_4.text = _loc_3.title;
                _loc_5.text = _loc_3.from;
                _loc_6.text = "";
                _loc_9.visible = false;
                this._itemArr[param2] = param1;
                param1.addEventListener(MouseEvent.CLICK, this.choseEmail);
                _loc_8.addEventListener(MouseEvent.CLICK, this.delEmail);
                if (param2 == 0)
                {
                    this.showEmail(_loc_3);
                }
            }
            return;
        }// end function

        public function changeState(param1:Array) : void
        {
            var _loc_3:Object = null;
            var _loc_4:int = 0;
            var _loc_2:* = new Object();
            for each (_loc_3 in param1)
            {
                
                _loc_4 = 0;
                while (_loc_4 < this._emailList.length)
                {
                    
                    if (_loc_3.emailId == this._emailList[_loc_4].emailId)
                    {
                        this._emailList[_loc_4].state = _loc_3.state;
                    }
                    if (elist.array[_loc_4])
                    {
                        if (_loc_3.emailId == elist.array[_loc_4].emailId)
                        {
                            _loc_3.index = elist.array[_loc_4].index;
                            elist.changeItem(_loc_4, _loc_3);
                            _loc_2 = _loc_3;
                        }
                    }
                    _loc_4++;
                }
            }
            this.showEmail(_loc_2);
            return;
        }// end function

        public function deleteEmail(param1:Array) : void
        {
            var _loc_6:Boolean = false;
            var _loc_7:int = 0;
            this.clear();
            var _loc_2:Array = [];
            var _loc_3:* = new Object();
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            while (_loc_5 < this._emailList.length)
            {
                
                _loc_6 = true;
                for each (_loc_7 in param1)
                {
                    
                    if (_loc_7 == this._emailList[_loc_5].emailId)
                    {
                        _loc_6 = false;
                    }
                }
                if (_loc_6)
                {
                    _loc_3 = this._emailList[_loc_5];
                    _loc_3.index = _loc_4;
                    _loc_4++;
                    _loc_2.push(_loc_3);
                }
                _loc_5++;
            }
            this._emailList = _loc_2;
            this.getYe();
            return;
        }// end function

        public function addEmail(param1:Object) : void
        {
            this._isFirst = true;
            this._emailList.unshift(param1);
            this.getYe(this._mPage);
            return;
        }// end function

        public function checkEmail() : void
        {
            var _loc_1:Object = null;
            for each (_loc_1 in this._emailList)
            {
                
                if (_loc_1.state < 2)
                {
                    ModelProxy.change("mainui", "showemail", true);
                    break;
                }
            }
            return;
        }// end function

        public function play() : void
        {
            if (this.timeline)
            {
                this.timeline.kill();
            }
            btnSure.filters = [];
            this.timeline = TweenMax.to(btnSure, 1, {glowFilter:{color:16763904, alpha:1, blurX:6, blurY:6, strength:4}, repeat:int.MAX_VALUE, yoyo:true});
            return;
        }// end function

        public function stop() : void
        {
            if (this.timeline)
            {
                this.timeline.kill();
            }
            btnSure.filters = [];
            return;
        }// end function

    }
}
